const https = require('https');
const fs = require('fs');
const express = require('express');
const cors = require('cors');
const app = express();

const {SerIP,SerDev,videoDev} = require('./config.js');// 引入配置
const {parser} = require('./parser.js'); // 引入地磅解析函数
const {barrier} = require('./barrier.js');

// ssl注入app
const httpsServ = https.createServer({
    key: fs.readFileSync('./'+SerIP+'cert.key','utf8'),
    cert: fs.readFileSync('./'+SerIP+'cert.pem','utf8'),
}, app);


// websocket注入app
const expressWs = require('express-ws')(app,httpsServ);

/***ssl及websocket注入，可以写业务逻辑了***/

const wsContro = {
    firstWeightWebsocket : function(ws,req){
        let interval
        // 连接成功后使用定时器定时向客户端发送数据，同时要注意定时器执行的时机，要在连接开启状态下才可以发送数据
        interval = setInterval(() => {
            if (ws.readyState === ws.OPEN) {
                ws.send(JSON.stringify({"stable":true,"weight":"45.32"})); 
            } else {
                clearInterval(interval)
            }
        }, 100)
    },
    secondWeightWebsocket : function(ws,req){
        let interval
        // 连接成功后使用定时器定时向客户端发送数据，同时要注意定时器执行的时机，要在连接开启状态下才可以发送数据
        interval = setInterval(() => {
            if (ws.readyState === ws.OPEN) {
                ws.send(JSON.stringify({"stable":true,"weight":"14.16"})); 
            } else {
                clearInterval(interval)
            }
        }, 100)
    },
    thirdWeightWebsocket : function(ws,req){
        let interval
        // 连接成功后使用定时器定时向客户端发送数据，同时要注意定时器执行的时机，要在连接开启状态下才可以发送数据
        interval = setInterval(() => {
            if (ws.readyState === ws.OPEN) {
                ws.send(JSON.stringify({"stable":true,"weight":"18.98"})); 
            } else {
                clearInterval(interval)
            }
        }, 100)
    },
    fourthWeightWebsocket : function(ws,req){
        let interval
        // 连接成功后使用定时器定时向客户端发送数据，同时要注意定时器执行的时机，要在连接开启状态下才可以发送数据
        interval = setInterval(() => {
            if (ws.readyState === ws.OPEN) {
                ws.send(JSON.stringify({"stable":true,"weight":"49.4"})); 
            } else {
                clearInterval(interval)
            }
        }, 100)
    },
    vehicleRecoWebsocket : function(ws,req){return;
        let interval
        // 连接成功后使用定时器定时向客户端发送数据，同时要注意定时器执行的时机，要在连接开启状态下才可以发送数据
        interval = setInterval(() => {
            if (ws.readyState === ws.OPEN) {
                ws.send(JSON.stringify({"vehicleNo":"陕K12345","camerIndex":2}));
                ws.send(JSON.stringify({"vehicleNo":"陕K12345","camerIndex":1}));
                ws.send(JSON.stringify({"vehicleNo":"陕K23456","camerIndex":3}));
                ws.send(JSON.stringify({"vehicleNo":"陕K23456","camerIndex":4})); 
            } else {
                clearInterval(interval)
            }
        }, 100)
    },
};

app.ws('/firstWeightWebsocket', wsContro.firstWeightWebsocket);
app.ws('/secondWeightWebsocket', wsContro.secondWeightWebsocket);
app.ws('/thirdWeightWebsocket', wsContro.thirdWeightWebsocket);
app.ws('/fourthWeightWebsocket', wsContro.fourthWeightWebsocket);
app.ws('/vehicleRecoWebsocket', wsContro.vehicleRecoWebsocket);

app.get('/',function(req,res){
    res.send('hello dibang');
});

app.post('/weight/restartWeight',function(req,res){
    res.send('success');
});

httpsServ.listen(443,'0.0.0.0');
console.log('ok');